package io.github.junxworks.qt.modules.mm.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.github.junxworks.ep.auth.model.UserModel;
import io.github.junxworks.ep.core.exception.BusinessException;
import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.junx.core.util.StringUtils;
import io.github.junxworks.qt.modules.mm.dto.MmDatastreamCondition;
import io.github.junxworks.qt.modules.mm.dto.MmDatastreamDto;
import io.github.junxworks.qt.modules.mm.entity.MmDatastream;
import io.github.junxworks.qt.modules.mm.entity.MmDsFieldRela;
import io.github.junxworks.qt.modules.mm.mapper.MmDatastreamMapper;
import io.github.junxworks.qt.modules.mm.service.MmDatastreamService;
import io.github.junxworks.qt.modules.mm.vo.FieldVo;
import io.github.junxworks.qt.modules.mm.vo.MmDatastreamVo;

@Service
public class MmDatastreamServiceImpl implements MmDatastreamService {

	@Autowired
	private MmDatastreamMapper mmDatastreamMapper;

	@Override
	public List<MmDatastreamVo> queryList(MmDatastreamCondition condition) {
		return mmDatastreamMapper.queryList(condition);
	}

	@Override
	public MmDatastreamVo queryMmDatastreamById(Long id) {
		return mmDatastreamMapper.queryMmDatastreamById(id);
	}

	@Override
	@Transactional
	public void save(MmDatastreamDto dto) {
		if (StringUtils.notNull(dto.getDatastreamName())) {
			MmDatastreamVo _m = mmDatastreamMapper.queryMmDatastreamByName(dto.getDatastreamName());
			if (_m != null) {
				if (dto.getId() == null || !_m.getId().equals(dto.getId())) {
					throw new BusinessException("已经存在数据流名称[" + dto.getDatastreamName() + "]");
				}
			}
		}
		MmDatastream entity = new MmDatastream();
		BeanUtils.copyProperties(dto, entity);
		UserModel user = (UserModel) SecurityUtils.getSubject().getPrincipal();
		Long userId = user.getId();
		if (entity.getId() == null) {
			entity.setCreateTime(new Date());
			entity.setCreateUser(userId);
			entity.setStatus(RecordStatus.NORMAL.getValue());
			mmDatastreamMapper.insertWithoutNull(entity);
		} else {
			MmDatastream m = mmDatastreamMapper.selectEntityByID(MmDatastream.class, entity.getId());
			if (!userId.equals(m.getCreateUser())) {
				throw new BusinessException("不是创建者不能操作数据");
			}
			entity.setUpdateTime(new Date());
			entity.setUpdateUser(userId);
			mmDatastreamMapper.updateWithoutNull(entity);
			mmDatastreamMapper.deleteDsFieldRela(entity.getId());
		}
		if (dto.getFieldList() != null && !dto.getFieldList().isEmpty()) {
			mmDatastreamMapper.insertBatch(dto.getFieldList().stream().flatMap(f -> {
				MmDsFieldRela r = new MmDsFieldRela();
				r.setDsId(entity.getId());
				r.setFieldId(f);
				return Stream.of(r);
			}).collect(Collectors.toList()));
		}
	}

	@Override
	public List<FieldVo> queryFieldsByDsId(Long id) {
		return mmDatastreamMapper.queryFieldsByDsId(id);
	}

	@Override
	public MmDatastreamVo queryDataStreamByName(String dsName) {
		return mmDatastreamMapper.queryMmDatastreamByName(dsName);
	}

	@Override
	public String checkDsCanDelete(Long id) {
		String res="";
		String varRela = mmDatastreamMapper.queryVariableNamesByDsId(id);
		if(StringUtils.notNull(varRela)) {
			res+="关联变量["+varRela+"] ";
		}
		String expRela = mmDatastreamMapper.queryExpNamesByDsId(id);
		if(StringUtils.notNull(expRela)) {
			res+="关联表达式["+expRela+"]";
		}
		return res;
	}
}
